﻿using System.Collections.Generic;

namespace MLog.Core {
    public class CachedLoggerFactory : ILoggerFactory {
        private readonly Dictionary<string, ILogger> _loggerCache = new Dictionary<string, ILogger>();

        public ILoggerFactory DelegateFactory { get; set; }

        public CachedLoggerFactory(ILoggerFactory delegateFactory) {
            this.DelegateFactory = delegateFactory;
        }

        public ILogger GetLogger(string name) {
            if (_loggerCache.ContainsKey(name)) return _loggerCache[name];

            var logger = DelegateFactory.GetLogger(name);
            _loggerCache[name] = logger;
            return logger;
        }

        public IEnumerable<ILogger> GetCachedLoggers() {
            return new List<ILogger>(_loggerCache.Values);
        }

        public void Clear() {
            _loggerCache.Clear();
        }
    }
}